import re

from django.views import View
# 导入返回响应
from SaaSDevelTool.data_return_response import create_response
# 导入错误信息装饰器
from SaaSDevelTool.data_return_response import error_message_detection
# 导入分页器
from SaaSDevelTool.data_operation import paging_tool_join
# 查询当前参数是否是int类型
from SaaSDevelTool.data_operation import is_number

"""企业看板查询"""


class SaaSDataTenementQuery(View):
    @error_message_detection
    def get(self, request):
        # 获取分页的页码
        page_number = request.GET.get("page_number")
        # 获取分页的条数
        page_size = request.GET.get("page_size")
        # 获取的企业名称
        tenement_name = request.GET.get("tenement_name")
        # 判断参数
        if not is_number(page_number) or not is_number(page_size):
            return create_response(code=400, msg="页码与页面尺寸格式错误。")

        # 判断企业名称
        if tenement_name:
            # 拼接语句
            where_tenement_name_sql = """AND tenement_name LIKE '%{}%'""".format(tenement_name)
        else:
            where_tenement_name_sql = ""

        # 拼接sql语句
        query_sum_up_tenement_table_sql = """
            SELECT
                tenement_id,
                tenement_name,
                tenement_status,
                DATE_FORMAT(tenement_update_time,'%Y-%m-%d %H:%i:%s') as tenement_update_time,
                tenement_white_image,
                tenement_color_images,
                tenement_remark
            FROM
                db_saas_center.sum_up_tenement_table 
            WHERE 
                1=1 {}
            ;""".format(where_tenement_name_sql)
        # 调用方法获取数据
        query_sum_up_tenement_table_data = paging_tool_join(
            query_sum_up_tenement_table_sql,
            int(page_number),
            int(page_size))

        # 返回对应的数据
        return create_response(code=200, msg="", data=query_sum_up_tenement_table_data)
